﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using LdExpress;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public string ContainerPage = "./JobDescriptions.aspx";
    public string strCondition = "";
    public string strPager = "";
    public string strError = "";
    public string strMess = "";
    public string type = "";
    public int nPermissions = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int nPage = 1;
        string strPageID = "page_number";
        try
        {

            if (Request.QueryString[strPageID] != null)
                nPage = int.Parse(Request.QueryString[strPageID]);
        }
        catch
        {
            nPage = 1;
        }
        if (nPage <= 0) nPage = 1;

        if (Session["logined_id"] != null)
        {
            if (Session["JobDescriptions"] != "") nPermissions = int.Parse(Session["JobDescriptions"].ToString());
        }
        else
            Response.Redirect("Login.aspx");
        if (!Page.IsPostBack)
        {
            if (nPermissions < 9)
            {
                drAdminUser.Visible = false;
                btnLock.Visible = false;
            }
            if (nPermissions < 3)
                btnShowHide.Visible = false;
            loadDropdownUser();
            LoadJobDescriptionContent();
            #region Build List
            int nNumOfPages = 0;

            DataTable dtList;
            //BuildList(NumOfItemsPerPage, nPage, out nNumOfPages, out dtList, null, null);
            if (nPage > nNumOfPages) nPage = 1;

            //rptData.DataSource = dtList;
            //rptData.DataBind();
            #endregion
            #region Build Pager
            CommonFunctions.BuildPager(Request, nNumOfPages, nPage, strPageID, ContainerPage, out strPager);
            #endregion
        }

    }
    protected void LoadJobDescriptionContent()
    {
        long userID = long.Parse(Session["logined_id"].ToString());
        long ID = 0;
        if (Session["JobchangeUserID"] != null && int.Parse(Session["JobchangeUserID"].ToString()) > 0)
        {
            userID = long.Parse(Session["JobchangeUserID"].ToString());
        }
        ID = JobDescriptions.GetIdByUserID(userID, GV.cn, GV.trans);
        JobDescriptions obj = new JobDescriptions();
        if (ID > 0)
        {
            obj.ID = ID;
            obj.Load(GV.cn, GV.trans);
            txtJobDescriptions.Text = obj.Description;
            lblDisplay.Text = obj.Description;
            if (obj.IsLock == 1)
            {
                btnLock.Text = "Unlock";
                btnShowHide.Visible = false;
                txtJobDescriptions.Visible = false;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        long userID = long.Parse(Session["logined_id"].ToString());
        long ID = 0;
        if (Session["JobchangeUserID"] != null && int.Parse(Session["JobchangeUserID"].ToString()) > 0)
        {
            userID = long.Parse(Session["JobchangeUserID"].ToString());
        }
        ID = JobDescriptions.GetIdByUserID(userID, GV.cn, GV.trans);
        JobDescriptions obj = new JobDescriptions();
        if (ID > 0)
        {
            obj.ID = ID;
            obj.Load(GV.cn, GV.trans);
            if (obj.IsLock == 1) return;
        }
        else
            obj.UserID = userID;
        obj.Description = txtJobDescriptions.Text;
        obj.Save(GV.cn, GV.trans);
        lblDisplay.Text = txtJobDescriptions.Text;
    }
    protected void drAdminUser_Selected(object sender, EventArgs e)
    {
        string sql = "";
        DataSet ds = null;
        string condition = "";
        Session["JobchangeUserID"] = drAdminUser.SelectedValue.ToString();
        //txtAdminUserID.Text = Session["changeUserID"].ToString();
        if (Session["superadmin"] != null && int.Parse(Session["superadmin"].ToString()) >= 7)
        {
            if (Session["JobchangeUserID"] != null && Session["JobchangeUserID"].ToString() != "0")
            {
                condition = " AND UserID=" + Session["JobchangeUserID"].ToString();
            }
            else
            {
                condition = "";
            }
        }
        else
        {
            condition = " AND UserID=" + Session["logined_id"].ToString();
        }
        Response.Redirect("JobDescriptions.aspx");
    }
    protected void btnLock_Click(object sender, EventArgs e)
    {
        long userID = long.Parse(Session["logined_id"].ToString());
        long ID = 0;
        if (Session["JobchangeUserID"] != null && int.Parse(Session["JobchangeUserID"].ToString()) > 0)
        {
            userID = long.Parse(Session["JobchangeUserID"].ToString());
        }
        ID = JobDescriptions.GetIdByUserID(userID, GV.cn, GV.trans);
        JobDescriptions obj = new JobDescriptions();
        int lockValue = 1;
        if (btnLock.Text == "Unlock")
        {
            lockValue = 0;
            btnLock.Text = "Lock";
        }
        else if (btnLock.Text == "Lock")
        {
            lockValue = 1;
            btnLock.Text = "Unlock";
        }

        if (ID > 0)
        {
            obj.ID = ID;
            obj.Load(GV.cn, GV.trans);
            obj.IsLock = lockValue;
            obj.Save(GV.cn, GV.trans);
            lblDisplay.Text = txtJobDescriptions.Text;
        }
    }
    protected void loadDropdownUser()
    {
        string sql = "";
        DataSet ds = null;
        sql = "SELECT ID, UserName FROM tbl_adminusers ORDER BY UserName";
        ds = DBModule.ExecuteQuery(sql, GV.cn, GV.trans);
        DataTable dtb = ds.Tables[0];
        drAdminUser.DataSource = dtb;
        drAdminUser.DataValueField = dtb.Columns[0].ToString();
        drAdminUser.DataTextField = dtb.Columns[1].ToString();
        drAdminUser.DataBind();
        //drAdminUser.Items.Insert(0, new ListItem("Select", "0"));
        if (Session["JobchangeUserID"] != null)
            drAdminUser.SelectedValue = Session["JobchangeUserID"].ToString();
        else
            drAdminUser.SelectedValue = Session["logined_id"].ToString();
    }

}
